package com.ranshiyan.controller;
//1
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ranshiyan.entity.Area;
import com.ranshiyan.entity.Student;
import com.ranshiyan.entity.Subject;
import com.ranshiyan.service.StuService;

@Controller
public class StuController {
	
	@Autowired
	StuService stuService;
	
	@RequestMapping("list")
	public String list(HttpServletRequest request) {
		List<Student> listStudent = stuService.listStudent();
		request.setAttribute("list", listStudent);
		return "list";
	}
	
	@RequestMapping("toadd")
	public String toadd(HttpServletRequest request) {
		List<Subject> listsubject = stuService.listsubject();
		//中国下面的省市县
		List<Area> listArea = stuService.listArea(1);
		request.setAttribute("listArea", listArea);
		request.setAttribute("subjectlist", listsubject);
		return "add";
	}
	
	@ResponseBody
	@RequestMapping("getArea")
	public List<Area> getArea(int id){
		List<Area> listArea = stuService.listArea(id);
		return listArea;
		
	}
	@RequestMapping("add")
	public String add(Student stu,int[] subjectId) {
		int add = stuService.add(stu, subjectId);
		return "redirect:list";
	}
	
	@RequestMapping("toupdate")
	public String toupdate(HttpServletRequest request,int id) {
		//获取单个学生用于回显
		Student student = stuService.findStudent(id);
		//获取中国下所有的县
		List<Area> listArea = stuService.listArea(1);
		//获取所有课程
		List<Subject> listsubject = stuService.listsubject();
		request.setAttribute("sub", listsubject);
		request.setAttribute("listArea", listArea);
		request.setAttribute("student", student);
		return "update";
	}
	
	@RequestMapping("update")
	public String update(HttpServletRequest request,int[] subjectId,Student student) {
		stuService.update(student, subjectId);
		return "redirect:list";
	}
}
